package com.example.workbook.controller;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

@RestController
public class ChShName {

    @RequestMapping("/read")
    public String read(String address) throws IOException {
        File flie = new File(address);
        String[] fileNameLists = flie.list();
        Workbook workbook0 = new HSSFWorkbook();
        Sheet sheet = workbook0.createSheet();
        Workbook workbook;
        int sum = 0;
        String s;
        FileInputStream fileInputStream;

        for (int i = 0; i < fileNameLists.length; i++) {
            s = fileNameLists[i];
            String suffix = s.substring(s.indexOf(".") + 1);
            switch (suffix) {
                case "xls": {
                    fileInputStream = new FileInputStream(address + "\\" + s);
                    workbook = new HSSFWorkbook(fileInputStream);
                    fileInputStream.close();
                    int j;
                    for (j = 0; j < workbook.getNumberOfSheets(); j++) {
                        Row row = sheet.createRow(sum + j);
                        row.createCell(0).setCellValue(fileNameLists[i]);
                        row.createCell(1).setCellValue(workbook.getSheetName(j));
                    }
                    sum += j + 1;
                    break;
                }

                case "xlsx": {
                    fileInputStream = new FileInputStream(address + "\\" + s);
                    workbook = new XSSFWorkbook(fileInputStream);
                    fileInputStream.close();
                    int j;
                    for (j = 0; j < workbook.getNumberOfSheets(); j++) {
                        Row row = sheet.createRow(sum + j);
                        row.createCell(0).setCellValue(fileNameLists[i]);
                        row.createCell(1).setCellValue(workbook.getSheetName(j));
                    }
                    sum += j + 1;
                    break;
                }

                default:
                    break;
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(address + "\\0000.xls");
        workbook0.write(fileOutputStream);
        fileOutputStream.close();
        return "OK";
    }

    @RequestMapping("/change")
    public String change(String address) throws IOException {
        FileInputStream fileInputStream;
        fileInputStream = new FileInputStream(address + "\\0000.xls");
        Workbook workbook0 = new HSSFWorkbook(fileInputStream);
        Sheet sheet = workbook0.getSheetAt(0);
        fileInputStream.close();
        Workbook workbook;
        int sum = 0;

        for (int i = 0; i < sheet.getLastRowNum(); i += sum) {
            Row row = sheet.getRow(i);
            String filename = row.getCell(0).getStringCellValue();
            String suffix = filename.substring(filename.indexOf(".") + 1);
            switch (suffix){
                case "xls":{
                    fileInputStream = new FileInputStream(address + "\\" + filename);
                    workbook = new HSSFWorkbook(fileInputStream);
                    fileInputStream.close();
                    int j;
                    for (j = 0; j < workbook.getNumberOfSheets(); j++) {
                        Row row1 = sheet.getRow(i + j);
                        Cell cell = row1.getCell(1);
                        cell.setCellType(CellType.STRING);
                        String sheetname = cell.getStringCellValue();
                        workbook.setSheetName(j, sheetname);
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(address + "\\" + filename);
                    workbook.write(fileOutputStream);
                    fileOutputStream.close();
                    sum = j + 1;
                    break;}

                case "xlsx":{
                    fileInputStream = new FileInputStream(address + "\\" + filename);
                    workbook = new XSSFWorkbook(fileInputStream);
                    fileInputStream.close();
                    int j;
                    for (j = 0; j < workbook.getNumberOfSheets(); j++) {
                        Row row1 = sheet.getRow(i + j);
                        Cell cell = row1.getCell(1);
                        cell.setCellType(CellType.STRING);
                        String sheetname = cell.getStringCellValue();
                        workbook.setSheetName(j, sheetname);
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(address + "\\" + filename);
                    workbook.write(fileOutputStream);
                    fileOutputStream.close();
                    sum = j + 1;
                    break;}

                default:
                    break;
            }
        }
        return "OK";
    }
}
